From: Boyuan Yang Date: Tue, 13 Aug 2024 16:48:28 +0000 (-0400) Subject: New upstream version 1.1.9+ds1 X-Git-Tag: archive/raspbian/1.1.9+ds1-4+rpi1^2^2~15^2 X-Git-Url: https://dgit.raspbian.org/%22http:/www.example.com/cgi/%22https:/%22bookmarks:///%22http:/www.example.com/cgi/%22https:/%22bookmarks:/?a=commitdiff_plain;h=90f7285a1f55e644490a9b0f09aab00f704931cf;p=opencc.git New upstream version 1.1.9+ds1 --- diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..70e3ef3 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1 @@ +github: BYVoid diff --git a/.github/workflows/bazel.yml b/.github/workflows/bazel.yml index f9ee858..5371675 100644 --- a/.github/workflows/bazel.yml +++ b/.github/workflows/bazel.yml @@ -16,5 +16,9 @@ jobs: steps: - uses: actions/checkout@v4 - uses: bazelbuild/setup-bazelisk@v3 + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: "3.12" - run: bazel build //:opencc - - run: bazel test --test_output=all //src/... //data/... //test/... + - run: bazel test --test_output=all //src/... //data/... //test/... //python/... diff --git a/.npmignore b/.npmignore index 8a56f3b..3db42ab 100644 --- a/.npmignore +++ b/.npmignore @@ -1,3 +1,4 @@ +.bazelversion .npmignore .gitignore CMakeLists.txt @@ -18,8 +19,9 @@ CMakeLists.txt /src/*TestBase.cpp /doc /data/scheme +/data/scripts /deps/google-benchmark -/deps/gtest* +/deps/googletest* /deps/pybind* /deps/tclap* /bazel-* diff --git a/BUILD.bazel b/BUILD.bazel index 6c61b49..263edc9 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -1,3 +1,6 @@ +load("@rules_cc//cc:defs.bzl", "cc_library") +load("@rules_python//python:py_library.bzl", "py_library") + package(default_visibility = ["//visibility:public"]) cc_library( @@ -17,3 +20,10 @@ cc_library( "//src:opencc", ], ) + +py_library( + name = "py_opencc", + deps = [ + "//python/opencc", + ], +) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3393613..1d8c8e6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -42,7 +42,7 @@ set (PACKAGE_URL https://github.com/BYVoid/OpenCC) set (PACKAGE_BUGREPORT https://github.com/BYVoid/OpenCC/issues) set (OPENCC_VERSION_MAJOR 1) set (OPENCC_VERSION_MINOR 1) -set (OPENCC_VERSION_REVISION 8) +set (OPENCC_VERSION_REVISION 9) if (CMAKE_BUILD_TYPE MATCHES Debug) set (version_suffix .Debug) diff --git a/MODULE.bazel b/MODULE.bazel index 8dd6621..b59448e 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -2,14 +2,29 @@ module( name = "opencc", - version = "1.1.8", + version = "1.1.9", compatibility_level = 1, ) bazel_dep(name = "darts-clone", version = "0.32") -bazel_dep(name = "googletest", version = "1.15.0") +bazel_dep(name = "googletest", version = "1.15.0", dev_dependency = True) bazel_dep(name = "marisa-trie", version = "0.2.6") +bazel_dep(name = "platforms", version = "0.0.10") +bazel_dep(name = "pybind11_bazel", version = "2.12.0") bazel_dep(name = "rapidjson", version = "1.1.0") bazel_dep(name = "rules_cc", version = "0.0.9") bazel_dep(name = "rules_python", version = "0.34.0") bazel_dep(name = "tclap", version = "1.2.5") + + +python = use_extension("@rules_python//python/extensions:python.bzl", "python") +python.toolchain( + python_version = "3.12", +) +pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip") +pip.parse( + hub_name = "pip", + python_version = "3.12", + requirements_lock = "//python/tests:requirements_lock.txt", +) +use_repo(pip, "pip") diff --git a/MODULE.bazel.lock b/MODULE.bazel.lock index cbb5f6a..e794d2d 100644 --- a/MODULE.bazel.lock +++ b/MODULE.bazel.lock @@ -152,6 +152,37 @@ }, "recordedRepoMappingEntries": [] } + }, + "@@pybind11_bazel~//:internal_configure.bzl%internal_configure_extension": { + "general": { + "bzlTransitiveDigest": "+F47SE20NlARCHVGbd4r7kkjg4OA0eCJcOd5fqKq4fQ=", + "usagesDigest": "iH2lKTfsNEpn2MqtGpBNwJrxbb2C7DiYmh/XuKgDtr8=", + "recordedFileInputs": { + "@@pybind11_bazel~//MODULE.bazel": "e6f4c20442eaa7c90d7190d8dc539d0ab422f95c65a57cc59562170c58ae3d34" + }, + "recordedDirentsInputs": {}, + "envVariables": {}, + "generatedRepoSpecs": { + "pybind11": { + "bzlFile": "@@bazel_tools//tools/build_defs/repo:http.bzl", + "ruleClassName": "http_archive", + "attributes": { + "build_file": "@@pybind11_bazel~//:pybind11-BUILD.bazel", + "strip_prefix": "pybind11-2.12.0", + "urls": [ + "https://github.com/pybind/pybind11/archive/v2.12.0.zip" + ] + } + } + }, + "recordedRepoMappingEntries": [ + [ + "pybind11_bazel~", + "bazel_tools", + "bazel_tools" + ] + ] + } } } } diff --git a/README.md b/README.md index e4edffb..6605453 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,21 @@ Discussion (Telegram): https://t.me/open_chinese_convert ## Installation 安裝 -See [Download](https://github.com/BYVoid/OpenCC/wiki/Download). +### Package Managers 包管理器 + +* [Debian](https://tracker.debian.org/pkg/opencc) +* [Ubuntu](https://launchpad.net/ubuntu/+source/opencc) +* [Fedora](https://packages.fedoraproject.org/pkgs/opencc/opencc/) +* [Arch Linux](https://archlinux.org/packages/extra/x86_64/opencc/) +* [Mac](https://formulae.brew.sh/formula/opencc) +* [Bazel](https://registry.bazel.build/modules/opencc) +* [Node.js](https://npmjs.org/package/opencc) +* [Python](https://pypi.org/project/OpenCC/) + +### Prebuilt 預編譯 + +* Windows (x86_64): [Latest build](https://ci.appveyor.com/api/projects/Carbo/opencc/artifacts/OpenCC.zip?branch=master&job=Environment:%20nodejs_version=none;%20Platform:%20x64) +* Windows (x86): [Latest build](https://ci.appveyor.com/api/projects/Carbo/opencc/artifacts/OpenCC.zip?branch=master&job=Environment:%20nodejs_version=none;%20Platform:%20x86) ## Usage 使用 @@ -64,7 +78,7 @@ See [demo.js](https://github.com/BYVoid/OpenCC/blob/master/node/demo.js) and [ts ### Python -[PyPI](https://pypi.org/project/OpenCC/) `pip install opencc` (Windows, Linux, Mac) +`pip install opencc` (Windows, Linux, Mac) ```python import opencc @@ -84,6 +98,8 @@ int main() { } ``` +[Full example with Bazel](https://github.com/BYVoid/opencc-bazel-example) + ### C ```c diff --git a/data/scripts/find_target.py b/data/scripts/find_target.py index 4244949..7b2b477 100755 --- a/data/scripts/find_target.py +++ b/data/scripts/find_target.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- import sys diff --git a/data/scripts/merge.py b/data/scripts/merge.py index 680c90e..01fcf51 100755 --- a/data/scripts/merge.py +++ b/data/scripts/merge.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- import codecs diff --git a/data/scripts/reverse.py b/data/scripts/reverse.py index 746f887..a87ae46 100755 --- a/data/scripts/reverse.py +++ b/data/scripts/reverse.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- import sys diff --git a/data/scripts/sort.py b/data/scripts/sort.py index f7d4dc5..6ad6e8c 100755 --- a/data/scripts/sort.py +++ b/data/scripts/sort.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- import sys diff --git a/data/scripts/sort_all.py b/data/scripts/sort_all.py index bd57ae7..c742413 100755 --- a/data/scripts/sort_all.py +++ b/data/scripts/sort_all.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- import glob import sys diff --git a/node/global.gypi b/node/global.gypi index 523e649..ad7f4ae 100644 --- a/node/global.gypi +++ b/node/global.gypi @@ -1,6 +1,6 @@ { "variables": { - "opencc_version": "1.1.8" + "opencc_version": "1.1.9" }, "target_defaults": { "defines": [ diff --git a/package.json b/package.json index 9f0f80d..83fe8a3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "opencc", - "version": "1.1.8", + "version": "1.1.9", "description": "Conversion between Traditional and Simplified Chinese", "author": "Carbo Kuo ", "license": "Apache-2.0", diff --git a/python/opencc/BUILD.bazel b/python/opencc/BUILD.bazel new file mode 100644 index 0000000..876b1ca --- /dev/null +++ b/python/opencc/BUILD.bazel @@ -0,0 +1,15 @@ +load("@rules_python//python:py_library.bzl", "py_library") + +package(default_visibility = ["//visibility:public"]) + +py_library( + name = "opencc", + srcs = ["__init__.py"], + data = [ + "//data/config", + "//data/dictionary:binary_dictionaries", + "//data/dictionary:text_dictionaries", + ], + imports = [".."], + deps = ["//src:py_opencc"], +) diff --git a/python/opencc/__init__.py b/python/opencc/__init__.py index f77f80c..3884bdc 100644 --- a/python/opencc/__init__.py +++ b/python/opencc/__init__.py @@ -3,13 +3,18 @@ from __future__ import absolute_import, unicode_literals import os import sys -from opencc.clib import opencc_clib +try: + import opencc_clib +except ImportError: + from opencc.clib import opencc_clib __all__ = ['OpenCC', 'CONFIGS', '__version__'] __version__ = opencc_clib.__version__ -_thisdir = os.path.dirname(os.path.abspath(__file__)) -_opencc_share_dir = os.path.join(_thisdir, 'clib', 'share', 'opencc') +_this_dir = os.path.dirname(os.path.abspath(__file__)) +_opencc_share_dir = os.path.join(_this_dir, 'clib', 'share', 'opencc') +_opencc_rootdir = os.path.abspath(os.path.join(_this_dir, '..', '..')) +_opencc_configdir = os.path.join(_opencc_rootdir, 'data', 'config') if sys.version_info.major == 2: text_type = unicode # noqa @@ -18,6 +23,8 @@ else: if os.path.isdir(_opencc_share_dir): CONFIGS = [f for f in os.listdir(_opencc_share_dir) if f.endswith('.json')] +elif os.path.isdir(_opencc_configdir): + CONFIGS = [f for f in os.listdir(_opencc_configdir) if f.endswith('.json')] else: CONFIGS = [] @@ -39,7 +46,9 @@ class OpenCC(opencc_clib._OpenCC): if not config.endswith('.json'): config += '.json' if not os.path.isfile(config): - config = os.path.join(_opencc_share_dir, config) + config_under_share_dir = os.path.join(_opencc_share_dir, config) + if os.path.isfile(config_under_share_dir): + config = config_under_share_dir super(OpenCC, self).__init__(config) self.config = config diff --git a/python/tests/BUILD.bazel b/python/tests/BUILD.bazel new file mode 100644 index 0000000..a8ca166 --- /dev/null +++ b/python/tests/BUILD.bazel @@ -0,0 +1,16 @@ +load("@pip//:requirements.bzl", "requirement") +load("@rules_python//python:py_test.bzl", "py_test") + +py_test( + name = "test_opencc", + srcs = ["test_opencc.py"], + data = [ + "//test/testcases", + ], + imports = [".."], + deps = [ + "//python/opencc", + requirement("pytest"), + requirement("exceptiongroup"), + ], +) diff --git a/python/tests/requirements_lock.txt b/python/tests/requirements_lock.txt new file mode 100644 index 0000000..330e274 --- /dev/null +++ b/python/tests/requirements_lock.txt @@ -0,0 +1,5 @@ +exceptiongroup==1.2.2 +iniconfig==2.0.0 +packaging==24.1 +pluggy==1.5.0 +pytest==8.3.2 diff --git a/python/tests/test_opencc.py b/python/tests/test_opencc.py index 8ca83b7..31b2b04 100644 --- a/python/tests/test_opencc.py +++ b/python/tests/test_opencc.py @@ -1,6 +1,9 @@ from __future__ import unicode_literals import os +import pytest +import sys + from glob import glob _this_dir = os.path.dirname(os.path.abspath(__file__)) @@ -39,3 +42,7 @@ def test_conversion(): for text, ans in zip(intexts, anstexts): assert converter.convert(text) == ans, \ 'Failed to convert {} for {} -> {}'.format(pref, text, ans) + + +if __name__ == "__main__": + sys.exit(pytest.main(sys.argv[1:])) diff --git a/src/BUILD.bazel b/src/BUILD.bazel index 550d3b8..e1e5f24 100644 --- a/src/BUILD.bazel +++ b/src/BUILD.bazel @@ -1,4 +1,6 @@ +load("@pybind11_bazel//:build_defs.bzl", "pybind_extension") load("@rules_cc//cc:defs.bzl", "cc_library") +load("@rules_python//python:py_library.bzl", "py_library") package(default_visibility = ["//visibility:public"]) @@ -328,6 +330,18 @@ cc_library( ], ) +pybind_extension( + name = "opencc_clib", + srcs = ["py_opencc.cpp"], + deps = [":opencc"], +) + +py_library( + name = "py_opencc", + data = [":opencc_clib"], + imports = ["."], +) + cc_library( name = "segmentation", srcs = ["Segmentation.cpp"], diff --git a/src/Config.cpp b/src/Config.cpp index eee6e06..a8392b1 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -222,6 +222,10 @@ Config::Config() : internal(new ConfigInternal()) {} Config::~Config() { delete reinterpret_cast(internal); } +ConverterPtr Config::NewFromFile(const std::string& fileName) { + return NewFromFile(fileName, std::vector{}, nullptr); +} + ConverterPtr Config::NewFromFile(const std::string& fileName, const std::vector& paths, const char* argv0) { diff --git a/src/Config.hpp b/src/Config.hpp index 1737d01..6b744b9 100644 --- a/src/Config.hpp +++ b/src/Config.hpp @@ -37,9 +37,11 @@ public: ConverterPtr NewFromString(const std::string& json, const std::vector& paths); + ConverterPtr NewFromFile(const std::string& fileName); + ConverterPtr NewFromFile(const std::string& fileName, - const std::vector& paths = {}, - const char* argv0 = nullptr); + const std::vector& paths, + const char* argv0); private: void* internal; diff --git a/src/SimpleConverter.cpp b/src/SimpleConverter.cpp index 7b24ac6..b179751 100644 --- a/src/SimpleConverter.cpp +++ b/src/SimpleConverter.cpp @@ -55,7 +55,7 @@ struct InternalData { paths_with_runfiles.push_back( bazel_runfiles->Rlocation("_main/data/dictionary")); return new InternalData( - config.NewFromFile(configFileName, paths_with_runfiles)); + config.NewFromFile(configFileName, paths_with_runfiles, argv0)); } #endif return new InternalData(config.NewFromFile(configFileName, paths, argv0)); diff --git a/src/tools/CommandLine.cpp b/src/tools/CommandLine.cpp index e029d3d..568d1bb 100644 --- a/src/tools/CommandLine.cpp +++ b/src/tools/CommandLine.cpp @@ -197,7 +197,7 @@ int main(int argc, const char* argv[]) { outputFileName = Optional(outputArg.getValue()); noFlush = true; } - converter = config.NewFromFile(configFileName, pathArg.getValue()); + converter = config.NewFromFile(configFileName, pathArg.getValue(), argv[0]); bool lineByLine = inputFileName.IsNull(); if (lineByLine) { ConvertLineByLine();